<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2019/12/18
 * Time: 10:38
 */
namespace App\Common\Utils;
class JsonResponse {
    
    // 处理操作，返回data
    public static function success($data=[],$msg='成功'){
        // 成功是1
        return [
            'code' => CODE_SUCCESS,
            'msg'  => $msg,
            'data'  => $data,
        ];
    }

    // 处理操作，不返回data
    public static function operate($msg='操作成功'){
        // 成功是1
        return [
            'code' => CODE_SUCCESS,
            'msg'  => $msg
        ];
    }

    // 返回列表数据
    public static function get($data=[],$page = false,$total = 0, $limit=10, $msg=''){
        if($page == true){
            $result =  [
                'code' => CODE_SUCCESS,
                'msg'  => $msg,
                'total' => $total,
                'limit' => $limit,
                'data'  => $data,
            ] ;
        }else{
            $result =  [
                'code' => CODE_SUCCESS,
                'msg'  => $msg,
                'data'  => $data,
            ] ;
        }
        return $result;
    }

    public static function get2($data=[],$page = false,$total = 0,$msg='',$type=1,$has_homework=0){
        if($page == true){
            $result =  [
                'has_homework' => $has_homework,
                'type' => $type,
                'code' => CODE_SUCCESS,
                'msg'  => $msg,
                'total' => $total,
                'data'  => $data,
                
            ] ;
        }else{
            $result =  [
                'code' => CODE_SUCCESS,
                'msg'  => $msg,
                'data'  => $data,
            ] ;
        }
        return $result;
    }

    // 错误
    public static function error($msg='失败',$code = CODE_ERROR){
        // 错误是9,    // 没权限是101
        return [
            'code' => $code,
            'msg'  => $msg,
        ];
    }

    // 没认证
//    public static function unauthorized($msg='未授权'){
//        // 错误是9,    // 没权限是101
//        return new JsonModel( [
//            'code' => CODE_UNAUTHORIZED,
//            'msg'  => $msg,
//        ] );
//    }
}